#!/bin/sh
####
##	dw-task-remind v0.1 :: dokuwiki task reminder script
#	Copyright (c) 2007 CCC Munich, http://muc.cccc.de/dw-task-remind
#
# Conf
#
export USERNAME='INSERT USERNAME HERE'
export PASSWORD='INSERT PASSWORD HERE';
export LOGIN_URL='https://kapsel.muccc.de/doku.php';
export TASKS_PAGE='intern:start'
export COOKIE_FILE='/tmp/dw-task-remind.cookie'
export USERS_FILE='/var/www/vhosts/kapsel.muccc.de/htdocs/conf/users.auth.php'
export WGET=`which wget`
export DATE=`date +%Y/%m/%d -d "2 days"`
#
# bootstrap
#
export PATH="~/bin:$PATH:.";
perl -e '' || (
  echo "$0 ERROR: Can't find perl interpreter in $PATH!" 1>&2
  echo "Add your perl path to the list at the top of this $0 file." 1>&2
  exit 1
)
perl -x "$0" $@
exit $?
#
# initialize
#
#!perl
use strict;
use Net::SMTP;
use LWP::UserAgent;
my $username 	= $ENV{'USERNAME'  };
die "no env USERNAME" unless $username;
my $password 	= $ENV{'PASSWORD'  };
die "no env PASSWORD" unless $password;
my $loginUrl 	= $ENV{'LOGIN_URL' };
die "no env LOGIN_URL" unless $loginUrl;
my $tasksPage 	= $ENV{'TASKS_PAGE' };
die "no env TASKS_PAGE" unless $tasksPage;
my $usersFile	= $ENV{'USERS_FILE'};
die "no env USERS_FILE" unless $usersFile;
my $cookieFile	= $ENV{'COOKIE_FILE'};
die "no env COOKIE_FILE" unless $cookieFile;
my $date	= $ENV{'DATE'	   };
die "no env DATE" unless $date;

my %users;
open( USERS, $usersFile ) or die "cant open $usersFile: $!";
while( <USERS> ) {
	 if( $_=~/^([^\#\:]+)\:.*\:([^\:]+)\:[^\:]+$/o ){
		$users{$1}=$2;
	}
}
close( USERS );

my $userAgent = LWP::UserAgent->new;
$userAgent->cookie_jar({ file => $cookieFile });
$userAgent->timeout(60);
$userAgent->agent('dw-task-remind');
my $http = $userAgent->get(
$loginUrl . "?id=$tasksPage&do=login&u=$username&p=$password&remember_me=0&submit=Login"
);
my $tasks=$http->content;
unlink( $cookieFile );
die "can't fetch tasklist" unless $tasks;

my @tasks = split( /\<tr\ class\=\"priority/o, $tasks);
foreach my $task ( @tasks ) {
	if( $task =~ /
		^(\d+)\"\>\s*\n
		\s*\<td\ class\=\"page\"\>\<ul\>\<li\>\<a\ href\=\"\/([^\"]+)\"[^\>]+\>([^\<]+)\<[^\n]+\n
		\s*\<td\ class\=\"date\"\>([\d\/]+)(?:\ \d+\:\d+)?\<\/td\>\s*\n
		\s*\<td\ class\=\"user\"\>([^\<]+)\<\/td\>\s*\n
		\s*\<td\ class\=\"status\"\>([^\<]+)\<\/td\>\s*\n
		\s*\<\/tr\>/ixo
	) {
		my $prio	= $1;
		my $url		= $2;
		my $desc	= $3;
		my $due		= $4;
		my @users	= split( /\s*(?:and|und|mit|plus|\+|vs|with|[\;\,\s])+\s+/, $5);
		my $status	= $6;
		$url =~ s/doku\.php/$loginUrl/;

		next unless $due eq $date;
		foreach my $user ( @users ) {
			next unless $users{$user};

			my $mailto  = $users{$user};
			my $subject = "TASK REMIND[prio$prio]: $desc";
			my $body    = "Hello $user,\n" .
				      "\n" .
				      "you have a(n) $status priority $prio task, which is due at $date.\n" .
				      "Please take the neccessary steps for fulfilling your commitment.\n" .
				      "\n" .
				      "Further information: $url\n" .
				      "\n" .
				      "Please inform all stakeholders immediatly,\n" .
				      "if an successfull outcome it not likely to happen in-time.\n" .
				      "This enables mitigation of risks as a group effort.\n" .
				      "\n" .
				      "Cheers,\n" .
				      "--\n" .
				      "dw-task-remind script";

			my $smtp = Net::SMTP->new( 'localhost', 'Timeout' => 60 );
			$smtp->mail( $ENV{'USER'}  );
			$smtp->to(   $users{$user} );
			$smtp->data();
			$smtp->datasend("Subject: $subject\n$body\n");
			$smtp->datasend();
			$smtp->quit();
		}
	}
}
